import { reqGetSearchInfo } from '@/api/index.js'

// search模块的小仓库
const state = {
  searchInfo: {}
};

const mutations = {
  GETSEARCHINFO(state, searchInfo) {
    state.searchInfo = searchInfo
  }
};

const actions = {
  // 获取search模块数据
  async getSearchInfo({ commit }, params) {
    // 当前reqGetSearchInfo()在调用时至少传入一个参数(空对象)
    // params参数是当用户派发action的时候，作为第二个参数传递过来的，至少是一个空对象
    let result = await reqGetSearchInfo(params);
    if (result.code === 200) {
      commit("GETSEARCHINFO", result.data)
    }
  },
};

// 计算属性
// 项目中getters的作用是，简化仓库中的数据
const getters = {
  attrsList(state) {
    return state.searchInfo.attrsList || [];
  },
  goodsList(state) {
    return state.searchInfo.goodsList || [];
  },
  trademarkList(state) {
    return state.searchInfo.trademarkList || [];
  }
};


export default {
  state,
  mutations,
  actions,
  getters

}